package per.mnxc.common;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dm.model.v20151123.SingleSendMailRequest;
import com.aliyuncs.dm.model.v20151123.SingleSendMailResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @BelongsProject: spring_stu
 * @BelongsPackage: per.mnxc.common
 * @Author: mnxc
 * @CreateTime: 2022-11-18  14:44
 * @Description: 邮件推送服务
 * @Version: 1.0
 */
public class EmailUtil {
    private static String AccessKeyId = "LTAI5tK3kK5zXQCz5EgWEANy";
    private static String AccessKeySecret = "LfnrSttBhxB3XcKw82dHZafvoe27f6";

    /**
     * 发送短信
     * @param subject 主题
     * @param templateCode 模板
     * @param toAddress 目标邮箱
     * @param code 验证码
     */
    public static void sendEmail(String subject, String templateCode,String toAddress,Integer code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyId, AccessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        SingleSendMailRequest request = new SingleSendMailRequest();

        //使用https加密连接
        request.setSysProtocol(ProtocolType.HTTPS);

        try {
            request.setAccountName("service@mnxc.xyz");
            request.setFromAlias("MNXC");
            request.setAddressType(1);
            request.setTagName("MNXC");
            request.setReplyToAddress(true);
            request.setToAddress(toAddress);//目标地址
            request.setSubject(subject);//邮件主题
            request.setHtmlBody(templateCode+"\n你的验证码为："+code+"请妥善保管.");
            SingleSendMailResponse httpResponse = client.getAcsResponse(request);
        }catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
